package tr.gov.tubitak.bilgem.esya.certviewer.actions;

import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;
import tr.gov.tubitak.bilgem.esya.certviewer.EImageManager;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 07.12.2011
 * Time: 11:18
 * To change this template use File | Settings | File Templates.
 */
public class ECertExportToFileAction extends AbstractAction{

    byte [] certData;
    public ECertExportToFileAction(byte[] certData) {
        super(Bundle.getString("ECertExportToFileAction.ExportToFile"),EImageManager.getInstance().getImageIcon("Save.png","Save_As_Icon"));
        this.certData = certData;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //To change body of implemented methods use File | Settings | File Templates.
        JFileChooser fileChooser = new JFileChooser();
        int option = fileChooser.showSaveDialog(null);
        File saveFile = fileChooser.getSelectedFile();
        if ((option == JFileChooser.APPROVE_OPTION )&& (saveFile != null)) // only do something if user chose 'ok'
        {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(saveFile);
                fos.write(certData);
                fos.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (IOException e1) {
                e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}
